import { defineStore } from 'pinia'
import {computed, reactive, toRefs} from 'vue'
import { version } from '../../package.json'
export interface AppState {
    uuid: string
    userInfo: {
        lang:string
        avatar: string
        nickName: string | null
        userId: string | null
        unionId: string | null
        openId: string | null
        mobile: string
    }
    token: string | null
}
export const useAppStore = defineStore(
    'app',
    () => {
        const state = reactive<AppState>({
            uuid: '',
            userInfo: {
                lang:'en',
                avatar: '',
                nickName: '',
                userId: '',
                unionId: '',
                openId: '',
                mobile: '',
            },
            token: null,
        })
        const isLogin = computed(() => Boolean(state.token))

        const isGetUserInfo = computed(
            () => Boolean(state.userInfo.nickName) || Boolean(state.userInfo.avatar)
        )
        const logout = () => {
            state.token = null
            state.userInfo = {
                lang:'en',
                avatar: '',
                nickName: null,
                userId: null,
                unionId: null,
                openId: null,
                mobile: '',
            }
            state.uuid = ''
        }

        return {
            /** State */
            ...toRefs(state),

            /** Getter */
            isLogin,
            isGetUserInfo,

            /** Action */
            logout,
        }
    },
    {
        /** 启用缓存 */
        persist: {
            key: 'app-store-' + version,
            storage: localStorage,
        },
    }
)
